﻿$(function () {
    $.jgrid.defaults = $.extend($.jgrid.defaults,
    {
        datatype: 'json',
        mtype: 'POST',
        rowList: [5, 10, 20, 50],
        viewrecords: true,
        autowidth: true,
        height: "100%",
        sortorder: "desc",
        //rownumbers : true,
        multiselect: true,
        loadui: "block",
        loadError: function (xhr, status, error) {
            Alert("错误", "发生错误请重试！");
        }
    });
    $.jgrid.edit = $.extend($.jgrid.edit,
    {
        afterSubmit: function (response, postdata) {
            var json = jQuery.parseJSON(response.responseText);
            return [json.success, json.message];
        },
        closeAfterEdit: true,
        closeAfterAdd: true,
        bottominfo: '<span style="color:red">*<span>表示必填'
    });
    $.jgrid.search = $.extend($.jgrid.search,
    {
        closeAfterSearch: true,
        multipleSearch: true
    });

    $("button, input:submit,.jqbutton").button();
}); 
 (function($){
    $.jgridx = 
    {
        selectColModel : 
        {
            editable : true,
            edittype : "select",
            editoptions : 
            {
                buildSelect : function(data){
                    var json = jQuery.parseJSON(data)
                    var v = json.value
                    var st = "<select>";
                    for(var i in v){
                        st += "<option value='" + v[i].key + "'>" + v[i].value + "</option>";
                    }
                    st += "</select>";
                    return st;
                }
            },
            editrules : 
            {
                required : true
            },
            formoptions : 
            {
                elmsuffix : '<span style="color:red">*<span>'
            },
            
            stype : "select",
            searchrules : 
            {
                required : true
            },
            searchoptions : 
            {
                sopt : ["eq"],
                
                buildSelect : function(data){
                    var json = jQuery.parseJSON(data)
                    var v = json.value
                    var st = "<select><option>请选择</option>";
                    for(var i in v){
                        st += "<option value='" + v[i].key + "'"
                        //if(i==0){st += "selected='selected'";}
                        st += ">" + v[i].value + "</option>";
                    }
                    st += "</select>";
                    return st;
                }
            }
        },
        selectCol : function(id, url){
            return $.extend(true, 
            {
                name : id,
                index : id,
                editoptions : 
                {
                    dataUrl : url
                },
                searchoptions : 
                {
                    dataUrl : url
                }
            }, $.jgridx.selectColModel);
        },
        selectNullCol : function(id, url){
            return $.extend(true, $.jgridx.selectCol(id, url),
            {
                editoptions : 
                {
                    buildSelect : function(data){
                        var json = jQuery.parseJSON(data)
                        var v = json.value
                        var st = "<select><option>请选择</option>";
                        for(var i in v){
                            st += "<option value='" + v[i].key + "'>" + v[i].value + "</option>";
                        }
                        st += "</select>";
                        return st;
                    }
                },
                formoptions : 
                {
                    elmsuffix : ''
                }
            });
        },
        dateColModel : 
        {
            align : "right",
            editable : true,
            edittype : "text",
            editoptions : 
            {
                dataInit : function(el){
                    $(el).datepicker();
                    //                            setTimeout(function () {
                    //                                $(el).datepicker();
                    //                            }, 200);
                }
            },
            editrules : 
            {
                date : true,
                required : true
            },
            formoptions : 
            {
                elmsuffix : '<span style="color:red">*<span>'
            },
            
            stype : "text",
            searchoptions : 
            {
                dataInit : function(el){
                    $(el).datepicker();
                },
                sopt : ["eq", "ge", "le"]
            },
            searchrules : 
            {
                date : true,
                required : true
            }
            
        },
        dateCol : function(id){
            return $.extend(true, 
            {
                name : id,
                index : id
            }, $.jgridx.dateColModel);
        },
        
        intColModel : 
        {
            align : "right",
            editable : true,
            editrules : 
            {
                required : true,
                number : true
            },
            formoptions : 
            {
                elmsuffix : '<span style="color:red">*<span>'
            },
            
            searchoptions : 
            {
                sopt : ["eq", "ge", "le"]
            },
            searchrules : 
            {
                required : true,
                number : true
            }
        },
        intCol : function(id){
            return $.extend(true, 
            {
                name : id,
                index : id
            }, $.jgridx.intColModel);
        },
        intNullCol : function(id){
            return $.extend(true, 
            {
                editrules : 
                {
                    required : false,
                    number : true
                },
                formoptions : 
                {
                    elmsuffix : ''
                }
            },
            $.jgridx.intCol(id)
            );
        },
        strColModel : 
        {
            editable : true,
            editrules : 
            {
                required : true
            },
            formoptions : 
            {
                elmsuffix : '<span style="color:red">*<span>'
            },
            
            searchoptions : 
            {
                sopt : ["in"]
            },
            searchrules : 
            {
                required : true
            }
        },
        strCol : function(id){
            return $.extend(true, 
            {
                name : id,
                index : id
            }, $.jgridx.strColModel);
        },
        strAutoCol : function(id, url){
            return $.extend(true, 
            {
                edittype : "text",
                editoptions : 
                {
                    dataInit : function(el){
                        $(el).click(function(){
                            $(el).autocomplete("search", "");
                        });
                        $(el).autocomplete({
                            source : url,
                            minLength : 0
                        });
                    }
                }
            }, $.jgridx.strCol(id));
        },
        strAuto1Col : function(id, url){
            return $.extend(true, 
            {
                edittype : "text",
                editoptions : 
                {
                    dataInit : function(el){
                        $(el).click(function(){
                            $(el).autocomplete("search", "");
                        });
                        $(el).autocomplete({
                            source : url,
                            minLength : 0,
                            select : function(event, ui){
                            //ui.item.lable
                            $(el).parent().parent().parent().find($("#temptr")).remove();
                            $(el).parent().parent().after('<tr id="temptr" class="FormData" rowpos="1"><td class="CaptionTD">物料名称</td><td class="DataTD">&nbsp&nbsp' + ui.item.label.split(" ")[1] +'</td></tr>');
                            }
                        });
                    }
                }
            }, $.jgridx.strCol(id));
        },
        customAutoCol : function(id, url, pid){
            return $.extend(true, 
            {
                editrules : 
                {
                    edithidden : true,
                    required : true
                },
                formoptions : 
                {
                    elmsuffix : '<span style="color:red">*<span>'
                },
                hidden : true,
                search : false,
                edittype : "custom",
                editoptions : 
                {
                    custom_element : function(value, options){
                        var el = document.createElement("input");
                        el.type = "text";
                        if(value != ""){
                            $.get(pid + value, function(data){
                                if( ! data.success){
                                    Alert("错误", data.message);
                                }
                                else{
                                    el.value = data.name;
                                }
                            })
                            //el.value = val;
                            $(el).data("id", value);
                        }
                        
                        $(el).attr("class", "FormElement ui-widget-content ui-corner-all");
                        $(el).click(function(){
                            $(el).autocomplete("search", "");
                        });
                        $(el).autocomplete({
                            source : url,
                            minLength : 0,
                            select : function(event, ui){
                                $(el).data("id", ui.item.id);
                            }
                        });
                        return el;
                    },
                    custom_value : function(elem, operation, value){
                        if(operation === 'get'){
                            if($(elem).val() == "" || $(elem).data("id") === undefined){
                                return "";
                            }
                            var d = $(elem).data("id").toString();
                            return d;
                        }
                        else if(operation === 'set'){
                            $.get(pid + value, function(data){
                                if( ! data.success){
                                    Alert("错误", data.message);
                                }
                                else{
                                    $(elem).val(data.name);
                                }
                            })
                            $(elem).data("id", value);
                        }
                    }
                }
            }, $.jgridx.strCol(id));
        },
        
        
        strNullCol : function(id){
            return $.extend(true, 
            {
                name : id,
                index : id
            }, $.jgridx.strColModel, 
            {
                editrules : 
                {
                    required : false
                },
                formoptions : 
                {
                    elmsuffix : ""
                }
            });
        },
        checkColModel : 
        {
            editable : true,
            edittype : "checkbox",
            editrules : 
            {
                required : true
            },
            search : false
        },
        checkCol : function(id){
            return $.extend(true, 
            {
                name : id,
                index : id
            }, $.jgridx.strColModel);
        }
    }
})(jQuery);
//    $(function(){
//     function ceshi(id, url){
//     return $.extend(true, { name: id, index: id, editoptions: { dataUrl: url }, searchoptions: { dataUrl: url} }, $.jgridx.selectColModel);
//     }
//     var a =ceshi("CarId", "/Car/Cars");
//     var s = 123;
//    });
